/*
 * Step_Three.cpp
 *
 *  Created on: 2011-6-17
 *      Author: ryan
 */
#include "Step_Three.h"

int PATH[4][2] = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 } };

void getProjection(int num) {
	int k, i, j, l;
	for (k = 0; k < num; ++k) {
		for (i = 1; i < out.width - 2; ++i) {
			for (j = 1; j < out.height - 2; ++j) {
				if (isPicked[i][j] == 1) {
					for (l = 0; l < 4; ++l) {
						isPicked[i + PATH[l][0]][j + PATH[l][1]] = 1;
					}
				}
			}
		}
	}
	for (i = 0; i < out.width; ++i) {
		for (j = 0; j < out.height; ++j) {
			color *c1 = &out.data[i][j];
			if(isPicked[i][j] == 1){
				color *c2 = &inp.data[i][j];
				c1->r = c2->r;
				c1->g = c2->g;
				c1->b = c2->b;
			}
		}
	}
}

void fileWriting() {
	WriteBmp(outFilePath, &out);
}

void finalize() {
	int i;
	for (i = 0; i < ref.width; ++i) {
		free(ref.data[i]);
		ref.data[i] = NULL;
	}
	free(ref.data);
	ref.data = NULL;
	for (i = 0; i < inp.width; ++i) {
		free(inp.data[i]);
		inp.data[i] = NULL;
		free(isPicked[i]);
		isPicked[i] = NULL;
		if(rank == 0){
			free(out.data[i]);
			out.data[i] = NULL;
		}
	}
	free(inp.data);
	inp.data = NULL;
	free(isPicked);
	isPicked = NULL;
	if(rank == 0){
		free(out.data);
		out.data = NULL;
	}
}

